package com.gotobject.junit.internal.runners.statements;

import static com.gotobject.junit.internal.Registry.get;
import com.gotobject.junit.internal.runners.model.Measure;
import com.gotobject.junit.internal.runners.model.Explosion;
import com.gotobject.junit.internal.runners.model.PerformanceKey;
import com.gotobject.junit.internal.Registry;

import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.util.List;

/**
 *
 * @author Huascar A. Sanchez (huascar.sanchez@gmail.com)
 */
public class FailOnResponseTimeoutExplosion extends FailOnExplosion {
	private final   List<Explosion>     data;
	private final   Description         testDesc;
	private         Measure             measure;


    /**
	 *
	 * @param next statement
	 * @param data to be utilized when running the test method.
	 * @param testDesc description of the test being run
	 */
	public FailOnResponseTimeoutExplosion(Statement next, List<Explosion> data, Description testDesc){
        super(next);
        this.data 	        = data;
		this.testDesc       = testDesc;
		measure             = new Measure(0L, 0L, 0L);
	}



    @Override
    public void evaluate() throws Throwable {
		for(int idx = 0; idx < 1000; idx ++){}   // warm up

		for(Explosion each : data){
            explode(each, true);
		}
	}

	@Override
    protected void capture(long min, long max, long avg){
		measure = new Measure(max, min, avg);
        get().register(testDesc, new Registry.ExecutionRecord(PerformanceKey.RESPONSE, measure));
	}

	@Override
    public Measure toMeasure(){
		return new Measure(measure);
	}
}
